package com.flaskpy.web.eternity.admin.config;

import cn.hutool.core.util.ClassUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import com.flaskpy.web.eternity.admin.entity.Menu;
import com.flaskpy.web.eternity.core.cache.RedisForValueCache;
import com.flaskpy.web.eternity.core.cache.abs.ICacheOperation;
import com.flaskpy.web.eternity.core.factory.RedisConfigFactory;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.*;

import javax.crypto.KeyGenerator;
import java.lang.reflect.Method;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@EnableCaching
@Configuration
public class CacheConfig{

     /* redis序列化配置
     * @param redisConnectionFactory
     * @return
     */
    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        return RedisConfigFactory.generateRedisTemple(redisConnectionFactory,Object.class);

    }
    @Bean("captchaCache")
    public ICacheOperation<String> captchaCache(RedisConnectionFactory redisConnectionFactory){
        return RedisConfigFactory.generateCacheOperation("captcha:",redisConnectionFactory,String.class);
    }


//
//    @Bean("redisManager")
//    public CacheManager cacheManager(RedisConnectionFactory factory){
//
//        FastJsonRedisSerializer<Object> objSerializer = new FastJsonRedisSerializer(Object.class);
//        StringRedisSerializer strSerializer = new StringRedisSerializer();
//        objSerializer.getFastJsonConfig().setSerializerFeatures(
//                SerializerFeature.WriteMapNullValue,        // 是否输出值为null的字段,默认为false,我们将它打开
//                SerializerFeature.WriteNullListAsEmpty,     // 将Collection类型字段的字段空值输出为[]
//                //SerializerFeature.WriteNullStringAsEmpty,   // 将字符串类型字段的空值输出为空字符串
//                //SerializerFeature.WriteNullNumberAsZero,    // 将数值类型字段的空值输出为0
//                SerializerFeature.WriteDateUseDateFormat,
//                SerializerFeature.DisableCircularReferenceDetect    // 禁用循环引用
//        );
//        objSerializer.getFastJsonConfig().setDateFormat("yyyy-MM-dd HH:mm:ss");
//        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
//                .entryTtl(Duration.ofSeconds(30))
//                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(strSerializer))
//                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(objSerializer))
//                .disableCachingNullValues();
//        RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
//                .cacheDefaults(config)
//                .build();
//        return cacheManager;
//    }
//    @Bean("redisKeyGenerator")
//    public KeyGenerator keyGenerator(){
//        return new KeyGenerator(){
//            @Override
//            public Object generate(Object target, Method method, Object... params) {
//                return String.format("%s.%s",method.getName(),params.length==0?"": Arrays.toString(params));
//            }
//        };
//
//    }






}
